
package com.gitee.jmash.websocket.grpc;

import com.gitee.jmash.common.utils.VersionUtil;
import com.gitee.jmash.core.grpc.cdi.GrpcService;
import com.gitee.jmash.core.lib.ProtoEnumUtil;
import com.gitee.jmash.core.utils.LogbackUtils;
import com.gitee.jmash.websocket.service.MessageService;
import com.google.protobuf.BoolValue;
import com.google.protobuf.Empty;
import com.google.protobuf.EnumValue;
import com.google.protobuf.Int32Value;
import com.google.protobuf.StringValue;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import jakarta.inject.Inject;
import java.util.List;
import java.util.Map;
import jmash.protobuf.CustomEnumValue;
import jmash.protobuf.CustomEnumValueMap;
import jmash.protobuf.Entry;
import jmash.protobuf.EntryList;
import jmash.protobuf.EnumEntryReq;
import jmash.protobuf.EnumValueList;
import jmash.protobuf.LogLevelReq;
import jmash.websocket.protobuf.AiChatMsgReq;
import jmash.websocket.protobuf.ChannelMsgReq;
import jmash.websocket.protobuf.ChannelUserMsgReq;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresAuthentication;

/**
 * Grpc服务实现.
 */
@GrpcService
public class WebsocketImpl extends jmash.websocket.WebsocketGrpc.WebsocketImplBase {

  private static Log log = LogFactory.getLog(WebsocketImpl.class);

  // 模块版本
  public static final String version = "v1.0.0";

  @Inject
  MessageService messageService;

  @Override
  public void version(Empty request, StreamObserver<StringValue> responseObserver) {
    responseObserver.onNext(
        StringValue.of(version + "-" + VersionUtil.snapshot(WebsocketImpl.class)));
    responseObserver.onCompleted();
  }

  @Override
  public void findEnumList(StringValue request, StreamObserver<EnumValueList> responseObserver) {
    try {
      List<EnumValue> list = ProtoEnumUtil.getEnumList(request.getValue());
      responseObserver.onNext(EnumValueList.newBuilder().addAllValues(list).build());
      responseObserver.onCompleted();
    } catch (Exception ex) {
      log.error("", ex);
      responseObserver.onError(Status.INTERNAL.withDescription(ex.getMessage()).asException());
    }
  }

  @Override
  public void findEnumMap(StringValue request,
      StreamObserver<CustomEnumValueMap> responseObserver) {
    try {
      Map<Integer, CustomEnumValue> values = ProtoEnumUtil.getEnumMap(request.getValue());
      responseObserver.onNext(CustomEnumValueMap.newBuilder().putAllValues(values).build());
      responseObserver.onCompleted();
    } catch (Exception ex) {
      log.error("", ex);
      responseObserver.onError(Status.INTERNAL.withDescription(ex.getMessage()).asException());
    }
  }

  @Override
  public void findEnumEntry(EnumEntryReq request, StreamObserver<EntryList> responseObserver) {
    try {
      List<Entry> entryList =
          ProtoEnumUtil.getEnumCodeList(request.getClassName(), request.getType());
      responseObserver.onNext(EntryList.newBuilder().addAllValues(entryList).build());
      responseObserver.onCompleted();
    } catch (Exception ex) {
      log.error("", ex);
      responseObserver.onError(Status.INTERNAL.withDescription(ex.getMessage()).asException());
    }
  }

  @Override
  @RequiresAuthentication
  public void updateLogLevel(LogLevelReq request, StreamObserver<BoolValue> responseObserver) {
    LogbackUtils.updateLevel(request);
    LogbackUtils.testLog(request);
    responseObserver.onNext(BoolValue.of(true));
    responseObserver.onCompleted();
  }

  @Override
  @RequiresAuthentication
  public void sendChannelMsg(ChannelMsgReq request, StreamObserver<Int32Value> responseObserver) {
    try {
      int count = messageService.sendChannelMsg(request);
      responseObserver.onNext(Int32Value.of(count));
      responseObserver.onCompleted();
    } catch (Exception ex) {
      log.error("", ex);
      responseObserver.onError(Status.INTERNAL.withDescription(ex.getMessage()).asException());
    }
  }

  @Override
  @RequiresAuthentication
  public void sendChannelUserMsg(ChannelUserMsgReq request,
      StreamObserver<BoolValue> responseObserver) {
    try {
      boolean result = messageService.sendChannelUserMsg(request);
      responseObserver.onNext(BoolValue.of(result));
      responseObserver.onCompleted();
    } catch (Exception ex) {
      log.error("", ex);
      responseObserver.onError(Status.INTERNAL.withDescription(ex.getMessage()).asException());
    }
  }

  @Override
  @RequiresAuthentication
  public void sendAIChatMsg(AiChatMsgReq request, StreamObserver<BoolValue> responseObserver) {
    try {
      boolean result = messageService.sendAIChatMsg(request);
      responseObserver.onNext(BoolValue.of(result));
      responseObserver.onCompleted();
    } catch (Exception ex) {
      log.error("", ex);
      responseObserver.onError(Status.INTERNAL.withDescription(ex.getMessage()).asException());
    }
  }

}
